var mark = {
    showMark: function (markid, type) {
        var allEntities = viewer.entities;
        if (markid == "All") {
            for (var k = 0; k < allEntities._entities._array.length; k++) {
                allEntities._entities._array[k].show = type;
            }
            //allEntities.show = type;
        } else {
            allEntities.getById(markid).show = type;
        }
    },
    addMarker: function (objEntity, isDisplay) {
        /*objEntity含有一下成员markid,name,description,x,y,z,iconPath, iconSize, FontName, r, g, b,FontSize,markType,mediaUrl
        * markid:标注id
        * name:标注显示的文字
        * des:描述，（description是原对象属性值，规避用des）
        * x,y,z：坐标（可以移动，本版本暂不支持修改）
        * iconPath:图标地址
        * iconSize:图标大小（暂定固定大小，若设置需两个值）
        * FontName:字体（）需和FontSize一同使用，'30px sans-serif'
        * r/g/b:new Cesium.Color(red, green, blue, alpha)(目前默认白色，启用则可以有甚多)
        * FontSize：如FontName
        * markType:标注类型
        * mediaUrl：媒体链接
        */
        var oNewMark = viewer.entities.add({
            name: objEntity.name,
            position: new LSGlobe.Cartesian3(objEntity.x, objEntity.y, objEntity.z),
            /*point : { //点
            pixelSize : 5,
            color : LSGlobe.Color.RED,
            outlineColor : LSGlobe.Color.WHITE,
            outlineWidth : 2
            },
            id:"",*/
            label: { //文字标签
                text: objEntity.name,
                font: (parseInt(objEntity.FontSize) * 2) + "px " + objEntity.FontName,
                style: LSGlobe.LabelStyle.FILL_AND_OUTLINE,
                outlineWidth: 6,
                translucencyByDistance: new LSGlobe.NearFarScalar(1.5e2, 1.0, 1.5e5, 0.0),
                horizontalOrigin: LSGlobe.HorizontalOrigin.LEFT,
                //verticalOrigin : LSGlobe.VerticalOrigin.BOTTOM, //垂直方向以底部来计算标签的位置
                pixelOffset: new LSGlobe.Cartesian2(15, -2),  //偏移量
                disableDepthTestDistance: 1000,
                scale: 0.5
            },
            billboard: { //图标
                image: objEntity.iconPath,
                width: 64,
                height: 64,
                disableDepthTestDistance: 1000,
                scale: 0.5,
                translucencyByDistance: new LSGlobe.NearFarScalar(1.5e2, 1.0, 1.5e5, 0.0)
            },
            id: objEntity.markid,
            des: objEntity.description,
            show: isDisplay,
            x: objEntity.x, y: objEntity.y, z: objEntity.z,
            markerType: objEntity.markType,
            mediaUrl: objEntity.mediaUrl,
            typerName: objEntity.typeName
        });
    },
    addMarkerOnMap: function (markerId, isDisplay, parentNode, sort) {
        var markerData = "", markerTabData = "";
        var url = basePath + "/getMarkByMarkGuid.action";
        var datas = "guid=" + markerId;
        $.ajax({
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    markerData = result.markers[0];
                } else {
                    console.error(result.resultMsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取单个标注信息失败" });
            }
        });

        var markPic = "images/icon-marker.png";

        switch (parentNode) {
            case "重点青少年":
                $("#zhongdianqingshaonian").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item zdqsn'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-zdqsn.png";
                break;
            case "精神障碍者":
                $("#jingshenzhangaizhe").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item jszaz'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-jszaz.png";
                break;
            case "刑满释放者":
                $("#xingmanshifangzhe").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item xmsfz'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-xmsfz.png";
                break;
            case "艾滋病患者":
                $("#aizibinghuanzhe").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item azbhz'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-azbhz.png";
                break;
            case "邪教人员":
                $("#xiejiaorenyuan").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item xjrk'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-xjrk.png";
                break;
            case "留守儿童":
                $("#liushouertong").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item lset'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-lset.png";
                break;
            case "流动人口":
                $("#liudongrenkou").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item ldrk'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-ldrk.png";
                break;
            //房屋管理                               
            case "危房监测":
                $("#house-wfjc").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item gwlf'><i class='img"+sort+"'></i><span>" + markerData.text + "</span></div>");
                markPic = sImg;
                break;
            //            case "待拆房屋":                               
            //                $("#house-dcfw").append(appendStr);                               
            //                break;                               
            //            case "出租房屋":                               
            //                $("#house-czfw").append(appendStr);                               
            //                break;                               
            //            case "高危物品存放所":                               
            //                $("#house-gwwp").append(appendStr);                               
            //                break;                               
            //事件处理                                
            case "游乐场安保":
                $("#event-xyab").append("<div data-event-type='1' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item xyab'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-xyab.png";
                break;
            case "交通管理":
                if (sort == 3 || sort == 4) {
                    $("#event-jtgl").append("<div data-event-type='1' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item jtgl'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    $("#event-jtgl").append("<div data-event-type='3' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item jtgl'><i></i><span>" + markerData.text + "</span></div>");
                }
                markPic = "images/mark/d-jtgl.png";
                break;
            case "违法犯罪":
                if (sort == 1 || sort == 3) {
                    $("#event-wffz").append("<div data-event-type='1' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item wffz'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    $("#event-wffz").append("<div data-event-type='2' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item wffz'><i></i><span>" + markerData.text + "</span></div>");
                }
                markPic = "images/mark/d-wffz.png";
                break;
            case "民政工作":
                $("#event-mzgz").append("<div data-event-type='3' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item mzgz'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-mzgz.png";
                break;
            case "禁毒管理":
                $("#event-jdgl").append("<div data-event-type='1' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item jdgl'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-jdgl.png";
                break;
            case "环境管理":
                $("#event-hjgl").append("<div data-event-type='2' data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hjgl'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hjgl.png";
                break;

            //网络列表                               
            case "网格一":
                if (sort == 3) {
                    markPic = "images/mark/d-bsxt.png";
                    $("#wangge1").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item bsxt'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    markPic = "images/mark/d-hsxt.png";
                    $("#wangge1").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                }
                break;
            case "网格二":
                if (sort == 2 || sort == 4) {
                    markPic = "images/mark/d-bsxt.png";
                    $("#wangge2").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item bsxt'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    markPic = "images/mark/d-hsxt.png";
                    $("#wangge2").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                }
                break;
            case "网格三":
                markPic = "images/mark/d-hsxt.png";
                $("#wangge3").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                break;
            case "网格四":
                if (sort == 5 || sort == 1) {
                    markPic = "images/mark/d-bsxt.png";
                    $("#wangge4").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item bsxt'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    markPic = "images/mark/d-hsxt.png";
                    $("#wangge4").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                }
                break;
            case "网格五":
                markPic = "images/mark/d-hsxt.png";
                $("#wangge5").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                break;
            case "网格六":
                if (sort == 2 || sort == 3) {
                    markPic = "images/mark/d-bsxt.png";
                    $("#wangge6").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item bsxt'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    markPic = "images/mark/d-hsxt.png";
                    $("#wangge6").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                }
                break;
            case "网格七":
                if (sort == 4) {
                    markPic = "images/mark/d-bsxt.png";
                    $("#wangge7").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item bsxt'><i></i><span>" + markerData.text + "</span></div>");
                } else {
                    markPic = "images/mark/d-hsxt.png";
                    $("#wangge7").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                }
                break;
            case "网格八":
                $("#wangge8").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格九":
                $("#wangge9").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格十":
                $("#wangge10").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格十一":
                $("#wangge11").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格十二":
                $("#wangge12").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格十三":
                $("#wangge13").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "网格十四":
                $("#wangge14").append("div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item hsxt'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-hsxt.png";
                break;
            case "户籍信息":
                $("#hujixinxi").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item zdqsn'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-zdqsn.png";
                break;
            case "智慧建筑":
                $("#house-zhjz").append("<div data-id='" + markerData.guid + "' title='请先勾选地图显示，再查看具体信息' class='work-child-item xyab'><i></i><span>" + markerData.text + "</span></div>");
                markPic = "images/mark/d-xyab.png";
                break;
        }

        var sPos = {
            markid: markerData.guid,
            name: markerData.text,
            description: '',
            x: markerData.x, y: markerData.y, z: markerData.z,
            iconPath: markPic,
            iconSize: 16, FontName: 'Microsoft YaHei',
            r: 256, g: 256, b: 256,
            FontSize: 16, markType: 1, mediaUrl: '',
            typeName: parentNode
        };
        //添加标注
        mark.addMarker(sPos, isDisplay);
    },
    loadMark: function (guid, isDisplay, parentNode) {
        var url = basePath + "/queryMarkNodebyParentGuid.action";
        var datas = "guid=" + guid;
        $.ajax({
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    var lists = result.list;
                    if (lists.length > 0) {
                        if (parentNode)
                            mark.addParentNode(parentNode, lists.length);

                        for (var s = 0; s < lists.length; s++) {
                            if (lists[s].nodeText.indexOf("倾角") != -1) {
                                sImg = "images/mark/qingjiao.png";
                            } else if (lists[s].nodeText.indexOf("北斗") != -1) {
                                sImg = "images/mark/beidou.png";
                            } else if (lists[s].nodeText.indexOf("裂缝") != -1) {
                                sImg = "images/mark/liefeng.png";
                            };
                            if (lists[s].markGuid) {
                                mark.addMarkerOnMap(lists[s].markGuid, isDisplay, parentNode, s);
                            } else {
                                mark.loadMark(lists[s].guid, isDisplay, lists[s].nodeText);
                            }
                        }
                    }
                } else {
                    console.error(result.resultMsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取标注失败" });
            }
        });
    },
    addParentNode: function (nodeName, sumNodeAmount) {
        switch (nodeName) {
            case "重点青少年":
                $("#zhongdianqingshaonian").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "精神障碍者":
                $("#jingshenzhangaizhe").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "刑满释放者":
                $("#xingmanshifangzhe").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "艾滋病患者":
                $("#aizibinghuanzhe").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "邪教人员":
                $("#xiejiaorenyuan").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "留守儿童":
                $("#liushouertong").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
            case "流动人口":
                $("#liudongrenkou").parent().find("span").html(nodeName + "(" + sumNodeAmount + ")");
                break;
        }

    },
    init: function (isDisplay) {
        // 加载标注列表
        mark.loadMark(dataGuid, isDisplay, "");
        //添加标注到场景
        /* loadMarkToSence(dataGuid); */
        //标注飞行到视点
        $(document).on("click", "#analysis-panel .layer-item.marker .layer-item-title", function () {
            var oThis = $(this);
            if (oThis.hasClass("flyto")) {
                var oPos = oThis.attr("camera-pos").split(",");
                var oUp = oThis.attr("camera-up").split(",");
                var oDir = oThis.attr("camera-dir").split(",");
                var sPos = { "x": oPos[0], "y": oPos[1], "z": oPos[2] };
                var sUp = { "x": oUp[0], "y": oUp[1], "z": oUp[2] };
                var sDir = { "x": oDir[0], "y": oDir[1], "z": oDir[2] };
                fly.flyToViewPoint(sPos, sDir, sUp);
            }
        });

        //二级单选框全选反选
        /*
        $(".work-submenu-child .check").click(function () {
        if ($(this).hasClass("active")) {
        //隐藏
        $(this).removeClass("active");
        $(this).parent().parent().find(".work-child-content").addClass("isHide");
        $(this).parent().parent().find(".work-child-item").attr("title","请先勾选地图显示，再查看具体信息");
        } else {
        //显示
        $(this).addClass("active");
        $(this).parent().parent().find(".work-child-content").removeClass("isHide");
        $(this).parent().parent().find(".work-child-item").attr("title", "");
        }
        });
        */

        /*标注显示隐藏*/
        $(document).on("click", "#analysis-panel .layer-item.marker .checkicon", function () {
            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                if (oThis.hasClass("box")) {
                    //隐藏改文件夹 所有的 marker
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").removeClass("active");
                    //文件夹数 
                    var oChildsDoc = oThis.parent().next().find(".layer-item-title.doc");
                    //标注数
                    var oChildsMarker = oThis.parent().next().find(".layer-item-title.flyto");
                    for (var a = 0; a < oChildsMarker.length; a++) {
                        var sGuid = oChildsMarker.eq(a).attr("id");
                        mark.showMark(sGuid, false);
                    }
                } else {
                    //隐藏改标注 
                    /* var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if(oParentBox.length<=0){
                    oThis.parents(".layer-item-content").prev().find(".checkicon").removeClass("active");
                    }*/
                    var oGuid = oThis.parent().attr("id");
                    mark.showMark(oGuid, false);
                }
            } else {
                oThis.addClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").addClass("active");
                    //文件夹数 
                    var oChildsDoc = oThis.parent().next().find(".layer-item-title.doc");
                    //标注数
                    var oChildsMarker = oThis.parent().next().find(".layer-item-title.flyto");
                    for (var a = 0; a < oChildsMarker.length; a++) {
                        var sGuid = oChildsMarker.eq(a).attr("id");
                        mark.showMark(sGuid, true);
                    }
                } else {
                    /* var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if(oParentChild==oParentBox.length){
                    oThis.parents(".layer-item-content").prev().find(".checkicon").addClass("active");
                    } */
                    var oGuid = oThis.parent().attr("id");
                    mark.showMark(oGuid, true);
                }
            }
            return false;
        });
    }
}



